

document.write("<div class=\"container\">\n" +
    "        <div class=\"navbar-header\">\n" +
    "            <button type=\"button\" class=\"navbar-toggle collapsed\" data-toggle=\"collapse\" data-target=\"#header-navbar-collapse\" aria-expanded=\"false\">\n" +
    "                <span class=\"sr-only\">实验楼</span>\n" +
    "                <span class=\"icon-bar\"></span>\n" +
    "                <span class=\"icon-bar\"></span>\n" +
    "                <span class=\"icon-bar\"></span>\n" +
    "            </button>\n" +
    "            <a class=\"navbar-brand\" href=\"#\">\n" +
    "                <img src=\"img/logo_03.png\">\n" +
    "            </a>\n" +
    "        </div>\n" +
    "        <div class=\"collapse navbar-collapse\" id=\"header-navbar-collapse\">\n" +
    "            <ul class=\"nav navbar-nav\">\n" +
    "                <li class=\"dropdown \">\n" +
    "                    <a href=\"javascript:void(0);\" class=\"dropdown-toggle\" data-toggle=\"dropdown\">\n" +
    "                        课程\n" +
    "                        <span class=\"caret\"></span>\n" +
    "                    </a>\n" +
    "                    <ul class=\"dropdown-menu\">\n" +
    "                        <li><a class=\"\" href=\"courses/index.html\" >全部课程</a></li>\n" +
    "                        <li><a class=\"\" href=\"courses/index.html?status=preview\" >即将上线</a></li>\n" +
    "                    </ul>\n" +
    "                </li>\n" +
    "\n" +
    "                <li class=\"\">\n" +
    "                    <a href=\"article/articleList.html\">博客中心</a>\n" +
    "                </li>\n" +
    "\n" +
    "                \n" +
    "            </ul>\n" +
    "\n" +
    "            \n" +
    "            <div class=\"navbar-right btns\" id=\"displayUser\">\n" +
    "                <a class=\"btn btn-default navbar-btn sign-in\" data-sign=\"signin\" href=\"#sign-modal\" data-toggle=\"modal\"  id=\"loginJump\" value=\"123\">登录</a>\n" +
    "                <a class=\"btn btn-default navbar-btn sign-up\" data-sign=\"signup\" href=\"#sign-modal\" data-toggle=\"modal\"  id=\"registerJump\">注册</a>\n" +
    "            </div>\n" +
    "            \n" +
    "\n" +
    "            <form class=\"navbar-form\" action=\"search\" method=\"get\" role=\"search\">\n" +
    "                <div class=\"form-group\">\n" +
    "                    <input type=\"text\" class=\"form-control\" id=\"search\" autocomplete=\"off\" placeholder=\"搜索 课程/问答\">\n" +
    "                    <a class=\"btn\"  href=\"#\" style=\"border-radius: 30px;background-color: white\" onclick=\"searchfor()\" >搜索</a>" +
    "                </div>\n" +
    "            </form>\n" +
    "        </div>\n" +
    "    </div>");


// function showuserName() {
//     let newUser = localStorage.getItem("User");
//
//     if (newUser == null) {
//         $("#userName").each(function () {
//             $(this).replaceWith('<b>' + '请先进行登录' + '</b>');
//         });
//
//     } else {
//         let user = JSON.parse(newUser);
//         let username = user.username;
//         console.log("newUser" + newUser);
//         console.log("username" + username);
//         $("#userName").each(function () {
//             $(this).replaceWith('<b>' + username + '</b>');
//         });
//         //隐藏登录框
//         $("#loginJump")[0].style.display = "none";
//         $("#registerJump")[0].style.display = "none";
//
//     }


infoLogin();

function infoLogin() {



    //console.log($.cookie("username"));
    // 从缓存中拿到 json对象并解析
    let user = JSON.parse(window.sessionStorage.getItem("User"));
    //let user = JSON.parse(window.localStorage.getItem("user"));
    console.log("======================================================");
    console.log(user)


    console.log($("#displayUser").html());
    // 定义登录成功显示模板
    let successHtml = "    <a class=\"dropdown-toggle btn btn-default navbar-btn  sign-up \" data-toggle=\"dropdown\" role=\"button\" aria-haspopup=\"true\" aria-expanded=\"false\">{{username}}<span class=\"caret\"></span></a>\n" +
        "    <ul class=\"dropdown-menu\" style=\"text-align: center;margin-right: 8% \">\n" +
        "        <li><a href=\"user/study.html\">个人中心</a></li>\n" +
        "        <li><a onclick=\"loginOut()\">退 出</a></li>\n" +
        "    </ul>";

    // 判断状态码是否为200
    if (user != null) {
        sessionStorage.setItem("userId", user.id);
        successHtml = successHtml.replace("{{username}}", user.username)
            .replace("{{userId}}", user.id);
        //$(".loginUser").html("<a href='#'>"+user.data.username+"</a>");
        console.log(successHtml);
        $("#displayUser").html(successHtml);
    }
}

/**
 * 保存用户id,并跳转个人信息页面
 * @param userId
 */
function setUserId(userId) {
    sessionStorage.setItem("userId", userId);
    location.href = "user/study.html";
}


/**
 * 退出登录
 */
function loginOut() {
    let flag_hand = confirm("退出系统");
    if (flag_hand == false){
        return;
    }

    sessionStorage.clear();
    httpGet("education_api/user/loginOut", null);

    // $(".changeLogin").html("<a href='login.html' class='loginUser'>登录</a></li>");
    $("#displayUser").html(
        "<a class=\"btn btn-default navbar-btn sign-in\" data-sign=\"signin\" href=\"#sign-modal\" data-toggle=\"modal\"  id=\"loginJump\" value=\"123\">登录</a>\" \n" +
        "        <a class=\"btn btn-default navbar-btn sign-up\" data-sign=\"signup\" href=\"#sign-modal\" data-toggle=\"modal\"  id=\"registerJump\">注册</a>");

    location.href = "http://localhost:8080/view_cilent/";
};

function searchfor() {
    let searchVal = $("#search").val();
    console.log("search:"+searchVal);
    location.href="courses/fuzzyquery.html?"+searchVal;

}


// //将JSON转为字符串存到变量里
// users = JSON.stringify(data);
// console.log("JSON-user:"+users);
// //将变量存到localStorage里
// localStorage.setItem("User",users);
//
// var newStudents = localStorage.getItem("students");
// newStudents = JSON.parse(students); //转为JSON
// console.log(newStudents); // 打印出原先对象


